#include <cstdio>
#include <cstring>

int N, M;
int FL[1005];
int CNT[1005];
bool VIS[1005][1005];

int main() {
	while (true) {
		scanf("%d", &N);
		if (N == 0) break;
		scanf("%d", &M);
		for (int i=1; i<=N; i++) {
			FL[i] = i;
		}
		memset(VIS, 0, sizeof(VIS));
		for (int i=0; i<M; i++) {
			int a, b;
			scanf("%d%d", &a, &b);
			if (VIS[a][b] || VIS[b][a]) continue;
			VIS[a][b] = VIS[b][a] = true;
			int fla = FL[a], flb = FL[b];
			for (int j=1; j<=N; j++) {
				if (FL[j] == flb) {
					FL[j] = fla;
				}
			}
		}
		memset(CNT, 0, sizeof(CNT));
		for (int i=1; i<=N; i++) {
			CNT[FL[i]]++;
		}
		int cnt1 = 0;
		for (int i=1; i<=N; i++) {
			if (CNT[i]) cnt1++;
		}
		printf("%d\n", cnt1-1);
	}
	return 0;
}
